package com.keyancompany.zhongbaopingtai.activity;

import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.utils.EditTextUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.CustomEditText;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * Created by Gia on 2016/8/12.
 */
public class RegisterActivity extends BaseActivity {
    @BindString(R.string.register)
    String title;

    @Bind(R.id.toolbar)
    android.support.v7.widget.Toolbar toolbar;
    @Bind(R.id.tel)
    CustomEditText tel;
    @Bind(R.id.pwd)
    CustomEditText pwd;
    @Bind(R.id.sms)
    CustomEditText sms;
    @Bind(R.id.text_getyzm)
    TextView textGetyzm;
    @Bind(R.id.accountname)
    CustomEditText accountname;
    @Bind(R.id.txt_register)
    TextView txtRegister;
    @Bind(R.id.single)
    RadioButton single;
    @Bind(R.id.organize)
    RadioButton organize;
    @Bind(R.id.role)
    RadioGroup role;

    int roleTag;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_register;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        role.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub

                if (checkedId == single.getId()) {
                    roleTag = 0;
                } else if (checkedId == organize.getId()) {
                    roleTag = 1;
                }
            }
        });
    }


    @OnClick({R.id.text_getyzm, R.id.txt_register})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.text_getyzm:
                if (EditTextUtils.getEditTextValue(tel) == null) {
                    T.showLong(mContext, R.string.login_tel_empty_hint);
                    return;
                }
                if (isNetvaiable()) {
                    UserAPI.getMscode(tel.getText().toString().trim(), RegisterActivity.this, getLoading(R.string.watting_hint));
                }
                break;
            case R.id.txt_register:

                if (EditTextUtils.getEditTextValue(tel) == null) {
                    T.showLong(mContext, R.string.login_tel_empty_hint);
                    return;
                }

                if (EditTextUtils.getEditTextValue(pwd) == null) {
                    T.showLong(mContext, R.string.login_password_empty_hint);
                    return;
                }
                if (EditTextUtils.getEditTextValue(sms) == null) {
                    T.showLong(mContext, R.string.msm_empty_hint);
                    return;
                }

                if (EditTextUtils.getEditTextValue(accountname) == null) {
                    T.showLong(mContext, R.string.accountname_empty_hint);
                    return;
                }
                if (isNetvaiable())
                    UserAPI.register(roleTag, tel.getText().toString().trim(), pwd.getText().toString().trim(), sms.getText().toString().trim(), pwd.getText().toString().trim(), RegisterActivity.this, getLoading(R.string.registering_watting_hint));
                break;
        }

    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);

        switch (type) {
            case UserAPI.REGISTER_CODE:

                if (validateJson(json) != null) {
                    BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                    T.showShort(mContext, base.msg);
                    if (base.result.equals(Constants.SUCCESS)) {
                        finish();
                    }
                    T.showShort(mContext, base.msg);
                }

                break;
            case UserAPI.GET_MSCODE_CODE:
                if (validateJson(json) != null) {
                    BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                    T.showShort(mContext, base.msg);
                    if (base.result.equals(Constants.SUCCESS)) {

                    }
                    T.showShort(mContext, base.msg);
                }

                break;

        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }

}
